package logic

import (
	"context"
	"errors"
	"login-hub/model/mysql"

	"login-hub/usersrpc/internal/svc"
	"login-hub/usersrpc/usersrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UsersCreateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUsersCreateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UsersCreateLogic {
	return &UsersCreateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UsersCreateLogic) UsersCreate(in *usersrpc.UsersCreateRequest) (*usersrpc.UsersCreateResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	err := user.UserCreate(mysql.DB, in.UsersId)
	if err != nil {
		return nil, errors.New("登录失败")
	}
	user.Price = float64(in.Price)
	user.Stock = int(in.Stock)

	err = user.UserCreateFind(mysql.DB)
	if err != nil {
		return nil, err
	}
	return &usersrpc.UsersCreateResponse{
		UsersId: int64(user.ID),
	}, nil
}
